package com.weiweiqin.controller;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weiweiqin.model.Category;
import com.weiweiqin.service.CategoryService;
import com.weiweiqin.vo.common.CategoryVO;
import com.weiweiqin.vo.common.Page;
import com.weiweiqin.vo.common.Result;

@Controller
@RequestMapping("/category")
public class CategoryController {

	private static final Logger logger = Logger
			.getLogger(CategoryController.class);
	private CategoryService categoryService;

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	@RequestMapping(value = "/add.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String add(Category category,HttpServletRequest request) throws UnsupportedEncodingException  {
		Result result = new Result();
		category.setUpdateDate(new Date());
		try {
			categoryService.save(category);
		} catch (Exception e) {
			logger.error("数据库异常...",e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}
	
	@RequestMapping(value = "/getAll.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String getAll()  {
		Result result = new Result();
		try {
			List<Category> categoryList = categoryService.getAll();
				result.setObj(categoryList);	
		} catch (Exception e) {
			logger.error("数据库异常...",e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}
	
	@RequestMapping(value = "/del.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String del(int id)  {
		Result result = new Result();
		try {
			categoryService.del(id);
		} catch (Exception e) {
			logger.error("数据库异常...",e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}
	
	@RequestMapping(value = "/pageList.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String pageList(int currPage,HttpServletResponse response) throws UnsupportedEncodingException  {
		Result result = new Result();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Page page = new Page();
			page.setCurrPage(currPage);
			List<Category> categoryList = categoryService.pageList(page, null);
	
			List<CategoryVO> list = new  ArrayList<CategoryVO>();
			if(null != categoryList && !categoryList.isEmpty()){
				
				for (Category category : categoryList) {
					CategoryVO vo = new CategoryVO();
					vo.setId(category.getId());
					vo.setName(category.getName());
					vo.setUpdateDate(sdf.format(category.getUpdateDate()));
					list.add(vo);
				}
			}
			int totalCount = categoryService.totalCount();
			page.setTotalCount(totalCount);
			Map<String,Object> mp = new HashMap<String, Object>();
			mp.put("categoryList", list);
			mp.put("totalPage",page.getTotalPage());
			result.setObj(mp);
		} catch (Exception e) {
			logger.error("数据库异常...",e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}
}
